package com.team7.happycommunity.businessservice.util;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 获取用户名
 */
@Component
public class CookieUtils {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisTemplate redisTemplate;

    public  String getName(){
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            for(Cookie cook:cookies){
                String value = cook.getValue();
                String name = cook.getName();
                String RedisName = (String)redisTemplate.boundValueOps(name).get();
                if (RedisName!=null&&RedisName.equals(value)){
                    return RedisName;
                }
            }
        }
       return null;
    }

}
